﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKElementFinish.ColumnSurface.DataDefinition;
using YJKElementFinish.ColumnSurface.Forms;
using YJKElementFinish.ColumnSurface.Managers;
using YJKRoomFixture.Kit;

namespace YJKRoomFixture.Forms
{
	public partial class RoomDecorateEntStructureForm : System.Windows.Forms.Form
	{
		public RoomDecorateEntStructureForm(Autodesk.Revit.DB.Document doc, List<WallLayerInfos> wallTypeLayerInfos, List<Element> lstElementType)
		{
			this.InitializeComponent();
			this.m_Doc = doc;
			this.m_CommonForm = new CompoundStructureCommonForm(base.Handle, doc, wallTypeLayerInfos);
			this.m_lstElementType = lstElementType;
		}

		public List<WallLayerInfos> WallLayerInfo
		{
			get
			{
				return this.m_CommonForm.CurrentWallLayerInfo;
			}
		}

		private void RoomDecorateEntStructureForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.m_CommonForm.TopLevel = false;
			this.m_CommonForm.Parent = this.panelMain;
			this.m_CommonForm.Show();
			this.edtTypeName.Text = this.m_strTypeName;
		}

		private void RoomDecorateEntStructureForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (((System.Windows.Forms.Form)sender).DialogResult != DialogResult.OK)
			{
				return;
			}
			List<WallLayerInfos> currentWallLayerInfo = this.m_CommonForm.CurrentWallLayerInfo;
			List<CompoundStructureLayer> list = new List<CompoundStructureLayer>();
			foreach (WallLayerInfos wallLayerInfos in currentWallLayerInfo)
			{
				list.Add(wallLayerInfos.StructureLayer);
			}
			string message = "";
			if (!WallTypeManager.IsValidWithLayers(list, this.m_Doc, ref message))
			{
				YJKMessageBox.Show(message);
				e.Cancel = true;
			}
			this.m_CommonForm.Update();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string text = this.edtTypeName.Text;
			if (text.IsEmpty<char>())
			{
				YJKMessageBox.Show("类型名称为空，请重新输入！");
				base.DialogResult = DialogResult.None;
				return;
			}
			this.m_strTypeName = text;
			if (this.IsTypeNameExist(this.m_lstElementType, text))
			{
				YJKMessageBox.Show("类型名称已经存在，请重新输入！");
				base.DialogResult = DialogResult.None;
				return;
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private bool IsTypeNameExist(List<Element> lstEntType, string strCurTypeName)
		{
			List<string> elementTypeNameList = DrawDecorateEnt.GetElementTypeNameList(lstEntType);
			return elementTypeNameList != null && elementTypeNameList.Count != 0 && elementTypeNameList.Contains(strCurTypeName);
		}

		private void RoomDecorateEntStructureForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private CompoundStructureCommonForm m_CommonForm;

		private Document m_Doc;

		public string m_strTypeName = "构件类型名称";

		private List<Element> m_lstElementType;
	}
}
